package com.zjhome.cloudnote.service.impl;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.zjhome.cloudnote.domain.NoteInfo;
import com.zjhome.cloudnote.mapper.NoteMapper;
import com.zjhome.cloudnote.service.NoteService;
import com.zjhome.cloudnote.util.ExplorerUtil;

@Service
public class NoteServiceImpl implements NoteService {
	
	@Autowired
	private NoteMapper	noteMapper;

	@Override
	public NoteInfo selectNoteById(String id, String uid) throws Exception {
		// 
		return noteMapper.selectNoteById(id, uid);
	}

	@Override
	public List<NoteInfo> selectAllNotesByUserid(String uid) throws Exception {
		// 
		return noteMapper.selectAllNotesByUserid(uid);
	}

	@Override
	public int insertNoteByParam(NoteInfo noteInfo) throws Exception {
		// 生成id
		if (StringUtils.isEmpty(noteInfo.getId())) {
			noteInfo.setId(UUID.randomUUID().toString());
		}
		return noteMapper.insertNoteByParam(noteInfo);
	}

	@Override
	public int updateNoteByParam(NoteInfo noteInfo) throws Exception {
		// 
		return noteMapper.updateNoteByParam(noteInfo);
	}

	@Override
	public int deleteNoteById(String id, String uid) throws Exception {
		// 
		return noteMapper.deleteNoteById(id, uid);
	}

	@Override
	public List<NoteInfo> selectSubNotesByDid(String did, String uid) throws Exception {
		// 
		return noteMapper.selectSubNotesByDid(did, uid);
	}

	@Override
	public List<NoteInfo> selectNotesByParam(NoteInfo noteInfo) throws Exception {
		// 
		return noteMapper.selectNotesByParam(noteInfo);
	}

	@Override
	public int updateNoteDelFlagByDir(String pid, String uid, int delflag) throws Exception {
		// 
		return noteMapper.updateNoteDelFlagByDir(pid, uid, delflag);
	}

	@Override
	public int deleteSubNoteById(String pid, String uid) throws Exception {
		// 
		return noteMapper.deleteSubNoteById(pid, uid);
	}

	@Override
	public List<NoteInfo> selectShareNotes(NoteInfo noteInfo) throws Exception {
		// 
		noteInfo.setShareflag(1);	// 置位分享标志
		
		return noteMapper.selectNotesByParam(noteInfo);		 
	}

	@Override
	public NoteInfo setNoteShareConfig(String id, String uid, int flag) throws Exception {
		// 
		// 获取 note 详情
		NoteInfo noteInfo = this.selectNoteById(id, uid);
		
		if (noteInfo.getShareflag() == flag) {			
			// 分享状态未改变，直接返回
			return noteInfo;
		}		
		
		if (flag == 1) {
			// 开启分享
			
			// 生成分享短路由
			while (true) {
				String shortUrl = ExplorerUtil.shortUrlGenerate();
				
				NoteInfo param = new NoteInfo();
				param.setShareflag(1);
				param.setShareurl(shortUrl);
				
				List<NoteInfo> noteList = this.selectNotesByParam(param);
				if (noteList.size() == 0) {
					// 没有发生碰撞，否则更改参数
					noteInfo.setShareflag(1);
					noteInfo.setShareurl(shortUrl);
					break;
				}
				// 否则继续尝试生成
			}
		}
		else {
			// 取消分享
			noteInfo.setShareflag(flag);
			noteInfo.setShareurl("");			
		}
		
		// 更新参数并返回
		this.updateNoteByParam(noteInfo);
		return noteInfo;
	}

	@Override
	public NoteInfo selectShareNote(String url) throws Exception {
		// 
		
		NoteInfo noteInfo = new NoteInfo();
		
		noteInfo.setShareflag(1);	// 开启分享
		noteInfo.setShareurl(url);	// 分享短路由
		
		List<NoteInfo> noteList = this.selectNotesByParam(noteInfo);
		if (noteList.size() == 0) {
			// 未找到有效的笔记数据
			return null;
		}
		else {
			return noteList.get(0);	// 返回第一个
		}
	}

}
